using System;
using System.Data;
using System.Diagnostics;
using Jandaya.Framework.DAO;
using FirebirdSql.Data.FirebirdClient;


namespace Jandaya.Framework.Core.Empresa
{
	public class DAOEmpresa: DAO<Empresa>
	{
		public DAOEmpresa(Empresa pBusinessObject, FbConnection pConnection)
		{
				
			BO = pBusinessObject;
			this.Connection = pConnection;			
		}
		
		public override bool Bind(DataRow pRow){			
			BO.ID.Value = Int64.Parse(pRow["id"].ToString());
			BO.Nome  = pRow["nome"].ToString();			
			BO.CNPJ = pRow["cnpj"].ToString();
			BO.NomeFantasia = pRow["nomeFantasia"].ToString();
    		BO.RazaoSocial = pRow["razaoSocial"].ToString();
    		BO.InscricaoMunicipal = pRow["inscMunicipal"].ToString();
	   		BO.InscricaoEstadual = pRow["inscEstadual"].ToString();
			BO.Ativo = (pRow["ativo"].ToString() == "S");
			
			
			//BO.Logotipo =
			/*
			FbCommand cmdGetLogo = GetCommand(string.Format("select logotipo from \"Core.Empresas\" where ID = {0}", BO.ID.Value));
			using(FbDataReader r = cmdGetLogo.ExecuteReader()){
				if(r.Read()){
					int bufferSize = 16;
					byte[] bytes = new byte[bufferSize];
					long count = r.GetBytes(0, 0, bytes, bufferSize);
				}
			}
			*/
			
			return true;
		}
		
		public override bool Load (Int64 ID)
		{
			
			DataTable tb = GetDataTable(string.Format("select first 1 ID, Nome, RazaoSocial, NomeFantasia, CNPJ, InscMunicipal, InscEstadual, Ativo from \"Core.Empresas\" where id = {0}", ID));
			if (tb.Rows.Count == 0)
				return false;
			else
				return Bind(tb.Rows[0]);
			
		}	
		
		public bool LoadDefault(){
			FbDataReader r = GetDataReader("select min(id) from \"Core.Empresas\"");
			r.Read();
			Int64 pId = r.GetInt64(0);
			r.Close();
			return this.Load(pId);
		}
		
		public override bool Save()
		{				
			FbCommand cmd = GetCommand("\"Core.Empresas.Set\"", CommandType.StoredProcedure);			
			
			cmd.Parameters.Add("ID", FbDbType.BigInt);
			cmd.Parameters.Add("Nome", FbDbType.VarChar, 80);
			cmd.Parameters.Add("RazaoSocial", FbDbType.VarChar, 120);
			cmd.Parameters.Add("NomeFantasia", FbDbType.VarChar, 120);
			cmd.Parameters.Add("CNPJ", FbDbType.VarChar, 20);
			cmd.Parameters.Add("InscMunicipal", FbDbType.VarChar, 20);
			cmd.Parameters.Add("InscEstadual", FbDbType.VarChar, 20);
			cmd.Parameters.Add("Ativo", FbDbType.Char, 1);
			cmd.Parameters.Add("IDPessoaJuridica", FbDbType.BigInt);
			cmd.Parameters["IDPessoaJuridica"].Direction = ParameterDirection.Output;
			                                   
			cmd.Parameters["ID"].Value  = BO.ID.Value;
			cmd.Parameters["Nome"].Value = BO.Nome;
			cmd.Parameters["RazaoSocial"].Value = BO.RazaoSocial;
			cmd.Parameters["NomeFantasia"].Value = BO.NomeFantasia;
			cmd.Parameters["CNPJ"].Value =  BO.CNPJ;
			cmd.Parameters["InscMunicipal"].Value = BO.InscricaoMunicipal;
			cmd.Parameters["InscEstadual"].Value = BO.InscricaoEstadual;
			cmd.Parameters["Ativo"].Value = (BO.Ativo) ? "S" : "N";							
			
			try{
				if (cmd.ExecuteNonQuery() == 0)
				return false;
				BO.ID.Value = Int64.Parse(cmd.Parameters["IDPessoaJuridica"].ToString());
				BO.SetState(ObjectState.Clean);
			}
			catch(FbException fbErr){
				Debug.WriteLine(fbErr.Message);
			}				
			return true;
		}
	}
}

